<template>
  <div>
    <div>
      <el-card style="height: auto">

        <el-form ref="ruleForm" label-width="100px" :model="ruleForm" :rules="rules" >
          <el-form-item label="密码" prop="password">
            <el-input placeholder="请输入密码"
                      v-model="ruleForm.password"
                      style="width: 200px"></el-input>
          </el-form-item>

          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input placeholder="请再次输入密码"
                      v-model="ruleForm.confirmPassword"
                      style="width: 200px"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="updatePassword('ruleForm')">修改密码</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>


export default {

  data() {
    return {
      rules: {
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],

        confirmPassword: [
          {required: true, message: '请确认密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
      },

      ruleForm: {
        password:'',
        confirmPassword:'',
      }
    }

  },

  methods: {


    updatePassword(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {


          if (this.ruleForm.password != this.ruleForm.confirmPassword) {

            this.$message({
              message: '两次密码不一致',
              type: 'error'
            });

            return;
          }

          let p = this.ruleForm.password;

          console.log(p);
          let id = localStorage.getItem("currentUserId");

          let url = 'http://localhost:9090/sys/user/personal/update/' + id + '/password';
          console.log('url = ' + url);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
              .post(url, p).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '修改用户成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else if (jsonResult.state == 40400) {
              this.$alert(jsonResult.message, '操作失败', {
                confirmButtonText: '确定',
                callback: action => {
                  this.resetForm(formName);
                }
              });
            } else {
              this.$alert(jsonResult.message, '操作失败', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          }).catch(error => {
            let title = '错误';
            let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
            this.$alert(message, title, {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
            console.log(error);
          });
        } else {
          console.log('请求参数格式错误！');
          return false;
        }
      });

    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }

}


</script>

<style>
.picture-uploader {
  float: left;
  width: 240px;
  height: 135px;
  margin-right: 20px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.picture-uploader .picture-uploader-icon {
  width: 240px;
  height: 80px;
  font-size: 58px;
  color: #8c939d;
  line-height: 80px;
  text-align: center;
}

.picture-uploader .picture-uploader-text {
  width: 240px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  font-size: 10px;
  color: #8c939d;
}

.picture-uploader .picture-uploader-text i {
  font-size: 10px;
  font-weight: bold;
  font-style: normal;
  color: #F56C6C;
}

.picture-preview {
  width: 240px;
  height: 135px;
  margin-right: 20px;
  border: 1px solid #e8e6e6;
  border-radius: 6px;
  float: left;
  display: inline;
  position: relative;
}

.picture-preview .image {
  width: 240px;
  height: 135px;
  border-radius: 6px;
}
</style>